package loteria.cliente;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.concurrent.Semaphore;

import loteria.Item;
import loteria.Observador;
import loteria.ObservadorHelper;
import loteria.Sorteo;
import loteria.SorteoHelper;
import loteria._ObservadorStub;
import loteria.servidor.Servidor;
import loteria.utils.Propiedades;

import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import com.sun.corba.se.impl.orbutil.concurrent.Mutex;
import com.sun.corba.se.org.omg.CORBA.ORB;

public class Client {

	/**
	 * @param args
	 */
	
	static final  String HOST = Propiedades.getValue("host");
	static final  String PORT = Propiedades.getValue("port");
	
	public static void main(String[] args) {
		

		Properties properties = System.getProperties();
	     properties.put("org.omg.CORBA.ORBInitialHost", Client.HOST );
         properties.put("org.omg.CORBA.ORBInitialPort", Client.PORT);

		try {
			// Initialize the ORB
			org.omg.CORBA.ORB orb = ORB.init(args, properties);

			// Get root POA instance
			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			// Activate root POA to accept requests
			rootpoa.the_POAManager().activate();

			// Get a reference of the name server
			NamingContextExt nc = NamingContextExtHelper.narrow(orb
					.resolve_initial_references("NameService"));

			// Get a CORBA ref of the calculator using the naming service
			org.omg.CORBA.Object obj = nc.resolve(nc.to_name("Sorteo"));

			// Cast (narrow) the CORBA object to Chat type
			Sorteo servidor = SorteoHelper.narrow(obj);

			ObservadorImpl observador = new ObservadorImpl();
			Observador ref = ObservadorHelper.narrow(rootpoa.servant_to_reference(observador));
			
			if (args.length != 1){
				System.out.println("Especifique la acción que desea realizar cliente.sh <observar/comprobar>");
			}else if (args[0].equals("observar")){
				Client client = new Client();
				servidor.registrar(ref);
				Thread read = client.new ReadThread(observador);
	            read.run();
			}else if (args[0].equals("comprobar")){
				
				
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				int numero = -1;
				while (numero != 0){
				try{
					System.out.print("Introduzca el numero que desea comprobar (0 para salir):");
					numero = Integer.parseInt(br.readLine());
					if(numero == 0) break;
					int premio = servidor.comprobar(numero);
					System.out.println("El número "+ numero+ " recibe "+premio+" € por cada decimo");
					
				}catch (Exception e) {
					System.out.println("Introduzca un numero entero correctamente, 0 para salir");
				}
				}
				
			}
			
			
			
			// Thread read = client.new ReadThread(chat, ref, nick);
			// read.start();
		} catch (Exception e) {
			System.out.println("ERROR : " + e);
			e.printStackTrace(System.err);
		}
	}
	class ReadThread extends Thread {
       
		ObservadorImpl observador = null;
		
        public ReadThread(ObservadorImpl _observador) {
        	this.observador = _observador;
        }

        public void run() {
                
                try {
                	this.observador.semaforo.acquire();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

}
